<template>
  <view>
    <u-navbar title="修改密码" ></u-navbar>
    <view>
      <view >
        <p style="background-color: #F8F8F8;height: 20rpx"></p>
      </view>
      <view style="padding:70rpx 50rpx 10rpx 50rpx">
        <u-input
            required
            trim
            type="password"
            v-model="oldPassword"
            placeholder="请输入原密码"
            label="原密码:"
            style="background: #F8F8F8"
            icon="lock-fill"
            icon-color="#40B6E0"
        >
        </u-input>
        <view style="margin-top: 20rpx">
          <u-input
              required
              trim
              type="password"
              :password="true"
              v-model="newPassword"
              placeholder="请输入新密码"
              label="新密码:"
              password-icon="true"
              icon="lock-open"
              icon-color="#40B6E0"
          >
          </u-input>
          </view>
        <view style="margin-top: 20rpx">
            <u-input
                required
                trim
                type="password"
                :password="true"
                v-model="newPasswordS"
                placeholder="请再次输入新密码"
                label="新密码:"
                password-icon="true"
                icon="lock-open"
                icon-color="#40B6E0"
            >
            </u-input>
        </view>

        <view style="color: #a0a1aa;padding-top: 20rpx">
          必须是6-20位英文字母,数字或符号(除空格)且新密码不能与原密码相同
        </view>

        <view style="margin-top: 40rpx">
          <u-button type="primary"  @click="clickOk">完成</u-button>
        </view>

      </view>
    </view>
  </view>
</template>

<script>
import {mineService} from '@/api/mine/mine.js';
import {
  http
} from '@/api/service.js'
import {UserService} from "../../../api/user";

export default {
  name: "setting",
  data() {
    return {
      oldPassword: '',
      newPassword:'',
      newPasswordS:"",
      passwordIcon: true,
      border: false,
    }
  },
  methods:{
    clickOk(){
      var reg =/^[a-zA-Z0-9,.-_+=!*@#$%^&*()?~]{6,20}$/;
      let _this=this;
      if(this.newPassword!=this.newPasswordS){
        uni.showToast({
          title: '两次输入的新密码不相同',
          icon: 'none'
        });
        return;
      }
      if (this.oldPassword!==''&&this.oldPassword!==null&&this.newPassword!==''&&this.newPassword!==null){
        if (reg.test(this.newPassword)){
          mineService.changePasswordByEid(this.oldPassword,this.newPassword).then(res=>{
            // console.log(res.data.code)
            if(res.data.code==1){
              uni.showToast({
                title: res.data.msg,
                icon: 'none',
              });
            }else {
              uni.showToast({
                title: res.data.msg,
                icon: 'none',
                success: function () {
                    UserService.logOut().then(res=>{
                      console.log("res",res)
                      if(res.statusCode==200){

                        //清空token，userInfo，tabList
                        uni.setStorageSync('token', null);
                        uni.setStorageSync('userInfo',null);
                        uni.setStorageSync('tabList',null);
                        _this.$store.state.socket.closeWebSocket();
                        _this.$store.dispatch("SET_SOCKET",null);
                        //退出后跳转至登录页
                        uni.redirectTo({
                          url:"/pages/public/login/login",
                          success:res =>{
                          },
                          fail:() =>{
                          },
                          complete:() => {
                          }
                        });
                      }
                    }).catch(err=>{
                      console.log("err,",err)
                    })
                }
              });
            }
          })
        }else {
          uni.showToast({
            title: '新密码格式不正确请重新输入',
            icon: 'none'
          });
        }
      }else {
        uni.showToast({
          title: '新旧密码不能为空',
          icon: 'none'
        });
      }
    }

  }
}
</script>

<style >
    .u-field{background-color:#F8F8F8 }
</style>
